package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"sync"
	"time"
)

func httpDo(url string) {

	// msg
	//msg := []byte(`{"condition_code":"12-2-192.168.90.2","connector_id":2,"message_type":0,"tenant_id":12,"local_ip":"192.168.90.2","version":"1.0.0.1","run_time":36000,"cpu":8,"memory":12,"cserver_connect":1,"cvpn_connect_success":9,"cvpn_connect_exception":0,"domain_resolution_failed":0,"session_count":2327,"dt_create":1645177420000}`)
	//msg := fmt.Sprintf("%u", time.Now().Unix())
	bTime, err := time.Now().MarshalBinary()
	if err != nil {
		fmt.Println(err)
		return
	}
	bodyBuf := bytes.NewBuffer(bTime)

	// 构建http客户端
	client := &http.Client{}
	req, err := http.NewRequest("POST",
		url,
		bodyBuf)
	if err != nil {
		fmt.Println("new request err : ", err)
		return
	}

	// 设置http头
	req.Header.Set("Content-Type", "application/json")

	// 发送http请求
	rsp, err := client.Do(req)
	if err != nil {
		fmt.Println("send req err : ", err)
		return
	}
	defer rsp.Body.Close()

	// 获取返回值
	body, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		fmt.Println("read rsp err : ", err)
		return
	}
	if rsp != nil {
		fmt.Println("rsp status : ", rsp.Status)
		fmt.Println("rsp code   : ", rsp.StatusCode)
		fmt.Println("rsp proto  : ", rsp.Proto)
		fmt.Println("rsp body   : ", string(body))
		var t time.Time
		err := t.UnmarshalBinary(body)
		if err != nil {
			fmt.Println("err")
			return
		}
		now := time.Now().UnixNano()
		fmt.Printf("rsp time   : %d now : %d ttl %dms\n", t.UnixNano(), now, (now - t.UnixNano())/1000000)
	}
}

func main() {
	wg := sync.WaitGroup{}
	for i := 0; i < 100; i++{
		go func(wg *sync.WaitGroup) {
			wg.Add(1)
			defer wg.Done()
			httpDo("http://82.157.166.118:9292/ping")
		}(&wg)
	}
	wg.Wait()
}